Use case: COMP window mode comparing by interruption

Description of usage

Operation

HAL1

HAL2

Start comparing

Start two comparators in window mode using HAL_COMP_Start_IT(&hcomp1) and HAL_COMP_Start_IT(&hcomp2).

Link the two comparators for window mode using HAL_COMP_WINDOW_SetHandle(&hcomp1, &hcomp2), then start the comparators using HAL_COMP_WINDOW_Start_IT(&hcomp1).

Comparator output trigger callback

Register the callback using HAL_COMP_RegisterCallback(), or implement HAL_COMP_TriggerCallback().

Register the callback using HAL_COMP_RegisterOutputTriggerCallback(), or implement HAL_COMP_OutputTriggerCallback().

Retrieve comparing result

Retrieve comparing results using HAL_COMP_GetOutputLevel().

Retrieve comparing results using HAL_COMP_WINDOW_GetOutputLevel().

Loop for multiple iterations

Possible multiple iterations if the comparator output has changed since the last read.

Possible multiple iterations if the comparator output has changed since the last read.

Stop comparing

Stop comparator analog features using HAL_COMP_Stop_IT(&hcomp1) and HAL_COMP_Stop_IT(&hcomp2).

Stop comparator analog features using HAL_COMP_WINDOW_Stop_IT(&hcomp1).

Note

Since the two comparators are linked, after calling this function both comparators will be stopped.

Sequence diagram

@startuml
title: COMP window mode operation using programming model: interruption

participant "User Application\n(thread mode)" as appli
participant "User Application\n(handler mode)" as appli_IRQ
participant "<font color=green><b>HAL COMP</b></font>" as COMP
participant "HAL EXTI" as EXTI
participant "System HW interruption" as interruption

group#Bisque #Linen COMP operation
appli->COMP : HAL_COMP_Start_IT(COMP1)
COMP->COMP : Enable selected interruptions\nActivate comparator analog features
COMP->appli

appli->COMP : HAL_COMP_Start_IT(COMP2)
COMP->COMP : Enable selected interruptions\nActivate comparator analog features
COMP->appli

loop#lightgrey Possible multiple iterations
interruption <[#red]-x? : <font color=red><b>COMP IRQ event
interruption -> appli_IRQ : COMPx_IRQHandler()
activate appli_IRQ #DarkSalmon
appli_IRQ -> COMP : HAL_COMP_IRQHandler()
COMP -> EXTI : Manage EXTI line flag
COMP <- EXTI
COMP -> appli_IRQ: HAL_COMP_OutputTriggerCallback()
appli_IRQ->COMP : HAL_COMP_GetOutputLevel(COMP1)
COMP --> appli_IRQ: Level below/within/above

appli_IRQ->COMP : HAL_COMP_GetOutputLevel(COMP2)
COMP --> appli_IRQ: Level below/within/above

appli_IRQ -> interruption : (return from ISR)
deactivate appli_IRQ
end
appli->COMP : HAL_COMP_Stop_IT(COMP1)
return
appli->COMP : HAL_COMP_Stop_IT(COMP2)
return
end

@enduml

Code snippet

HAL1

HAL2

int main(void)
{

  /*initialize and configure your comparing input*/




  /* Start the COMP  */
  HAL_COMP_Start_IT(&comp1);
  HAL_COMP_Start_IT(&comp2);

  while(HAL_COMP_GetOutputLevel(hcomp) != COMP_OUTPUT_LEVEL_HIGH)
  {

    /* generate voltage on comparator inputs */

    /* add a Wait time for voltage stabilization*/

  }
  /* Stop the COMP  */
  HAL_COMP_Stop_IT(&comp1);
  HAL_COMP_Stop_IT(&comp2);
}

void HAL_COMP_TriggerCallback(COMP_HandleTypeDef *hcomp)
{
  if ((hcomp->Instance == COMP1) || (hcomp->Instance == COMP2))
  {
    /* Handle the comparator event here */
    if (HAL_COMP_GetOutputLevel(hcomp) == COMP_OUTPUT_LEVEL_HIGH)
    {
      /* Comparator output is high */
    }
    else
    {
      /* Comparator output is low */
    }
  }
}
int main(void)
{

  /*initialize the comparator*/

  /*link the two comparators*/
  HAL_COMP_WINDOW_SetHandle(&hcomp1, &hcomp2);

  /*Configure your comparing input*/


  /* Start the COMP  */
  HAL_COMP_WINDOW_Start_IT(&comp1);

  while(HAL_COMP_WINDOW_GetOutputLevel(hcomp) != HAL_COMP_OUTPUT_LEVEL_HIGH)
  {

    /* generate voltage on comparator inputs */

    /* add a Wait time for voltage stabilization*/

  }
  /* Stop the COMP  */
  HAL_COMP_WINDOW_Stop_IT(&comp1);
}

void HAL_COMP_OutputTriggerCallback(COMP_HandleTypeDef *hcomp)
{
    /* Handle the comparator event here */
    if (HAL_COMP_WINDOW_GetOutputLevel(hcomp) == HAL_COMP_OUTPUT_LEVEL_HIGH)
    {
      /* Comparator output is high */
    }
    else
    {
      /* Comparator output is low */
    }
}

Key changes

  1. Rename the trigger callback.

  2. Rework COMP window mode.

Related specific concept: rework COMP Window mode.